package db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.log4j.Logger;
import model.Raha;




public class RahaDAO {
	
	  String sql;
	  Connection db;
	  static Logger logger = Logger.getLogger(RahaDAO.class);
	
	  public RahaDAO(Connection paramConnection)
	  {
	    try
	    {
	      this.db = paramConnection;
	    }
	    catch (Exception localException)
	    {
	      logger.error("RahaDAO:" + localException.getMessage());
	    }
	  }
	
	public Raha [] findAll() {
		Statement  st = null ;
		ResultSet RahaHulk  = null ;
		Raha [] RahaMassiiv = null   ;
		Raha Current_Raha  = null ;
		Connection db = null;	
		String sql ;
		int rahacount=0 ;
		try {

			db = DbConnection.getConnection();
			st = db.createStatement();

			sql = "select count(*) as kirjete_arv from Raha " ;
			RahaHulk = st.executeQuery(sql);

			while(RahaHulk.next()) {
				rahacount = RahaHulk.getInt("kirjete_arv");
			}

			sql = "SELECT id, nimetus, kurss FROM Raha ORDER BY id " ;
			RahaHulk = st.executeQuery(sql);
			RahaMassiiv = new Raha[rahacount];
			int cnt = 0;

			while(RahaHulk.next()) {
				Current_Raha = new Raha();    
				Current_Raha.setId(RahaHulk.getInt("id"));
				Current_Raha.setNimetus(RahaHulk.getString("nimetus"));
				System.out.println("getNimetus:" + Current_Raha.getNimetus() );
				Current_Raha.setKurss(RahaHulk.getInt("kurss"));
				RahaMassiiv[cnt] = Current_Raha ;
				cnt =  cnt + 1;
			}

		}

		catch(Exception ex)
		{
			logger.error("RahaDAO.findAll():"+ ex.getMessage());

		}

		finally
		{
			DbConnection.closeStatement(st);
			DbConnection.closeResultSet(RahaHulk);
			DbConnection.close(db);
		}

		return RahaMassiiv;
	}




	public Raha findById(int id) {
		Statement  st = null ;
		ResultSet RahaHulk  = null ;
		Raha Current_Raha  = null ;
		String sql ;
		Connection db  = null;	
		try {
			db = DbConnection.getConnection();
			st = db.createStatement();
			sql = "SELECT id, nimetus, kurss FROM raha WHERE id =" + Integer.toString(id) ;
			RahaHulk = st.executeQuery(sql);

			while(RahaHulk.next()) {    
				Current_Raha = new Raha();
				Current_Raha.setId(RahaHulk.getInt("id"));
				Current_Raha.setNimetus(RahaHulk.getString("nimetus"));
				System.out.println("getNimetus:" + Current_Raha.getNimetus() );
				Current_Raha.setKurss(RahaHulk.getInt("kurss"));
			
			}
		}

		catch(Exception ex)
		{
			logger.error("RahaDAO.findById():" + ex.getMessage());
ex.printStackTrace();
		}
		finally
		{
			DbConnection.closeStatement(st);
			DbConnection.closeResultSet(RahaHulk);
			DbConnection.close(db);
		}

		return Current_Raha ;
	}


	public void update(Raha updated_raha) {
		Statement  st = null ;
		Connection db  = null;	
		try {
			db = DbConnection.getConnection();
			st = this.db.createStatement();
			int i = st.executeUpdate("UPDATE raha SET nimetus='" + updated_raha.getNimetus() + "',kurss=" +  updated_raha.getKurss() + " WHERE id=" + Integer.toString(updated_raha.getId() ));
		}
		catch(Exception ex)
		{ 
			logger.error("RahaDAO.update():" + ex.getMessage());
			ex.printStackTrace();
		}
		finally
		{
			DbConnection.closeStatement(st);
			DbConnection.close(db);
		}

	}



	
	
}
